/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import com.bourre.structures.Range;
import com.bourre.transitions.IFrameListener;
import com.kairos.motion.MovieClipRT;
import com.kairos.motion.strategy.AdvancedLoopStrategy;
import com.kairos.motion.strategy.AdvancedReversedLoopStrategy;
import com.kairos.motion.strategy.IPlayStrategy;
import com.kairos.motion.AbstractAdvancedAnimation;
import com.bourre.log.PixlibStringifier;

/**
 * {@code AdvancedMovieClipRT} is dedicated to control {@code MovieClip} animations
 * in Kairos.
 * 
 * <p>With {@code AdvancedMovieClipRT} you can run a {@code MovieClip} only
 * on a specified nulber of loops, or in a specified range of frames.</p>
 * 
 * 
 * @author 	Cédric Néhémie
 * @see		AbstractAdvancedAnimation
 * @see 	IFrameListener 
 */
class com.kairos.motion.AdvancedMovieClipRT extends AbstractAdvancedAnimation implements IFrameListener 
{
	private var _m : MovieClip;
	
	/**
	 * Create a new {@code AdvancedMovieClipRT} witch handle
	 * the passed-in {@code MovieClip}.
	 * 
	 * <p>If {@code loopCount} is lower or equals than 0 then
	 * the animation run indefinitely.</p>
	 * 
	 * <p>If {@code range} is omitted the animation will play
	 * on the totality of the {@code MovieClip}'s frames.</p>
	 * 
	 * <p>If the <i>reverse</i> parameter is omitted the animation
	 * play from the first to the last frame.</p>
	 * 
	 * @param   mc	The MovieClip to work with.    
	 * @param   fps The speed of the animation.
	 * @param	loopCount Number of loops before stop.
	 * @param	range A valid {@code Range} object
	 * @param	r	{@code true} to make the movieclip playing reversed. 
	 * @example	var amc : AdvancedMovieClipRT = new AdvancedMovieClipRT ( myMc, 40 );
	 * @example	var amc : AdvancedMovieClipRT = new AdvancedMovieClipRT ( myMc, 40, 10);
	 * @example var amc : AdvancedMovieClipRT = new AdvancedMovieClipRT ( myMC, 40, 10, new Range( 5, 10 ) );	 * @example var amc : AdvancedMovieClipRT = new AdvancedMovieClipRT ( myMC, 40, 10, new Range( 5, 10 ), true );
	 */
	public function AdvancedMovieClipRT( mc : MovieClip, fps : Number, loopCount : Number, range : Range, r : Boolean ) 
	{
		super(fps, loopCount, range, r);
		setMovieClip( mc );
		setRange ( range );
		reset();
	}
	
	/**
	 * Defines a {@code MovieClip} to use with the object.
	 * 
	 * @param   mc	The target {@code MovieClip}.
	 */
	public function setMovieClip ( mc : MovieClip ) : Void
	{
		_m = mc;
		_tf = _m._totalframes;
		_f = _r ? 0 : _tf;
		_m.stop();
	}
	
	/**
	 * Returns the current {@code MovieClip} target
	 * 
	 * @return  The target {@code MovieClip}.
	 */
	public function getMovieClip () : MovieClip 
	{
		return _m;
	}
	
	/**
	 * Starts playing the MovieClip animation at the specified frame. 
	 * 
	 * @param	f	A number representing the frame number,
	 * or a string representing the label of the frame,
	 * to which the playhead is sent.
	 */
	public function gotoAndPlay ( f ) : Void
	{
		_f = f;
		_frameChanged ( _f );
		super.start();
	}
	
	/**
	 * Brings the playhead to the specified frame of the movie clip
	 * and stops it there. 
	 * 
	 * @param	f	A number representing the frame number,
	 * or a string representing the label of the frame,
	 * to which the playhead is sent.
	 */
	public function gotoAndStop ( f : Number ) : Void
	{
		_f = f;
		_frameChanged ( _f );
		super.stop();
	}

	
	/**
	 * Makes the composed {@code MovieClip} visible on the screen.
	 */
	public function show () : Void
	{
		_m._visible = true;
	}
	
	/**
	 * Makes the composed {@code MovieClip} invisible.
	 */
	public function hide () : Void
	{
		_m._visible = false;
	} 
	
	/**
	 * Called when frame changed according to internal computation.
	 */
	 private function _frameChanged ( f : Number ) : Void
	 { 
	 	_m.gotoAndStop( f >> 0 );
	 }
	 
	/**
	 * Return the String representation of the object. 
	 */	
	public function toString() : String
	{
		return PixlibStringifier.stringify( this );
	}
	
}